package cn.mw.service.impl;

import cn.mw.OpenFeign.MenuFeign;
import cn.mw.OpenFeign.UserFeign;
import cn.mw.domain.Menu;
import cn.mw.domain.Order;
import cn.mw.domain.User;
import cn.mw.dto.MenuDto;
import cn.mw.dto.OrdDto;
import cn.mw.mapper.OrderMapper;
import cn.mw.service.IOrderService;
import cn.mw.vo.OrderList;
import cn.mw.vo.OrderVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Resource
    private UserFeign userFeign;
    @Resource
    private MenuFeign menuFeign;
    @Value("${server.port}")
    private String port;
    /**
     * 下单业务
     * @param dto
     */
    @Override
    public void payment(OrdDto dto) {
        Long userid = dto.getUserid();
        //通过远程调用得到user对象
        User user = userFeign.getOne(userid);
        Order order = new Order();
        BigDecimal totalPrice = new BigDecimal(0);
        for (String item : dto.getMenuId()) {
            Menu menu = menuFeign.getOne(Long.valueOf(item));
            totalPrice = totalPrice.add(menu.getPrice());
        }
        // 商品id以","分割存储
        StringBuilder menuId = new StringBuilder();
        for (String id : dto.getMenuId()) {
            menuId.append(id);
            menuId.append(",");
        }
        order.setMenuId(menuId.toString());
        order.setPort(Integer.valueOf(port));
        order.setUserId(user.getId());
        order.setPhone(user.getPhone());
        order.setTotalPrice(totalPrice);
        orderMapper.saveOrder(order);
    }

    /**
     * 查询
     * @param userId
     * @return
     */
    @Override
    public OrderVo getOrderByUserId(Long userId) {

        // 查询用户-订单基本信息
        User user = orderMapper.getUserDetail(userId);

        List<OrderList> orderLists = new ArrayList<>();
        // 查询用户的订单
        List<Order> orders = orderMapper.getList(userId);
        // 集合的空校验
        if (!CollectionUtils.isEmpty(orders)){
            for (Order order : orders) {
                // 获取商品id
                String menuId = order.getMenuId();
                List<Menu> menus = new ArrayList<>();
                for (String id : menuId.split(",")) {
                    // 获取商品
                    Menu menu = menuFeign.getOne(Long.valueOf(id));
                    menus.add(menu);
                }
                OrderList orderList = OrderList.builder()
                        .orderId(order.getId())
                        .totalPrice(order.getTotalPrice())
                        .menuList(menus)
                        .build();
                orderLists.add(orderList);
            }
        }

        OrderVo orderVo = OrderVo.builder()
                .userName(user.getName())
                .phone(user.getPhone())
                .port(port)
                .orderList(orderLists)
                .build();
        return orderVo;
    }

    /**
     * 批量添加
     * @param menus
     */
    @Override
    public void addMenu(List<Menu> menus) {
        for (Menu menu : menus) {
            //调用feign接口保存
            menuFeign.getAdd(menu);
        }
    }

    public static void main(String[] args) {
        String strArr = "[0, 1. 2]";
        Object parse = JSONArray.parse(strArr);
        System.out.println(parse);

    }
}
